<%@page import="cz.acies.data.*"%>
<%@ page import="cz.acies.data.persist.*"%>
<%@ page import="cz.acies.face.modul.*"%>
<%@ page import="cz.acies.face.viewers.*"%>
<%@ page import="cz.acies.organizer.data.dao.*"%>
<%@ page import="cz.acies.organizer.data.persist.*"%>

<%@ page import="com.google.gson.*"%>

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
String jqxPath = path + "/libs/kendo";

//System.out.println("jqxPath: " + jqxPath); 

String title = application.getServletContextName();
String theme = (String) session.getAttribute("ApplicationTheme");
if (theme == null || theme.length() == 0) {
	//theme = "bootstrap";
	//theme = "default";
	//theme = "flat";
	theme = "metro";
	//theme = "metro-blue";
}
Map<String, Object> params = new HashMap<String, Object>();
Enumeration<String> e = request.getParameterNames();
while (e.hasMoreElements()) {
    String key =  e.nextElement();
    params.put(key, request.getParameter(key));
}
String locale = (String) session.getAttribute("locale");
if (locale == null || locale.length() == 0) { locale = "cs-CZ"; }
String modulId = (String) params.get("modulId");

Gson gson = new Gson();

DBConnector db = DBConnector.instance();  
ModulViewer viewer =  ModulViewer.create(modulId, db);

TreeViewer treeViewer = viewer.getTreeViewer();
String treeNode = gson.toJson((List<TreeNode>)treeViewer.getInput());

TableViewer tableViewer = viewer.getTableViewer();
List<TableColumnDefine> colDefs = tableViewer.getColumnDefs(); 

String serializerClass = "cz.acies.face.viewers.TableColumnDefine";
Object serializer = Class.forName(serializerClass).newInstance();
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(TableColumnDefine.class, serializer);
gson = builder.create();

String columns = gson.toJson(colDefs);
System.out.println(""+columns);

gson = new Gson();
String data = "";
/*
try {
	ContactRecordDAO dao = new ContactRecordDAO();
	List<ContactRecord> records = dao.getRecordsAll();
	List<Map<String, String>> list = new ArrayList<Map<String, String>>();
	for (ContactRecord record : records) {
		Map<String, String> row = new LinkedHashMap<String, String>();
		row.put("id", record.getId());
		String name = (record.getFirstName() + " " + record.getLastName());
		row.put("displayName", name);
		PostalAddress address = record.getHomeAddress();
		String s =
		(address.hasStreet() ? address.getStreet()+" " : "") +
		(address.hasCity() ? address.getCity()+" "  : "" ) +
		(address.hasRegion() ? address.getRegion()+" "  : "") +
		(address.hasPostCode() ? address.getPostCode()+" "  : "");
		row.put("address", ""+s.trim());
		
		EmailAddress email = record.getEmail();
		row.put("email", (email != null && email.getAddress() != null) ? ""+record.getEmail().getAddress() : "");
		PhonesNumber mobile = record.getMobile();
		row.put("mobile", (mobile != null) ? ""+mobile.getNumber() : "");
		list.add(row);
	}
	data = "" + gson.toJsonTree(list);
	System.out.println(""+data);
} catch (Exception exc) {
	exc.printStackTrace();
}
*/
data = "[]";
%>

<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>">
<title></title>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=8" />
  
<link href="<%=jqxPath%>/styles/kendo.common.css" rel="stylesheet" type="text/css">
<link href="<%=jqxPath%>/styles/kendo.rtl.css" rel="stylesheet" type="text/css">
<link href="<%=jqxPath%>/styles/kendo.<%=theme%>.css" rel="stylesheet" type="text/css"> 
  
<link href="<%=path%>/libs/themes/<%=theme%>/easyui.css" rel="stylesheet" type="text/css">
<link href="<%=path%>/libs/themes/icon.css" rel="stylesheet" type="text/css">
<style type="text/css">
html, body {
	width: 100%;
	height: 100%;
	margin: 0px;
	padding: 0px;
	overflow: hidden;
	font-size: 11px;
	font-family: sans-serif, Helvetica, Verdana;
}
</style>

<script type="text/javascript" src="<%=path%>/libs/jquery-1.9.1.js"></script>
<script type="text/javascript" src="<%=path%>/libs/aciea.utils.js"></script>
<script type="text/javascript" src="<%=path%>/libs/jquery.easyui.min.js"></script>
<script type="text/javascript" src="<%=path%>/libs/kendo/kendo.web.js"></script>
<script type="text/javascript">

</script>
</head>

<body>
	<div id="mainPanel" class="easyui-layout">
		<%=treeViewer.toHtml()%>
		<script type="text/javascript">
		function folderNew() {
			alert("folderNew()");			
		};
		
		function folderDelete() {
			alert("folderDelete()");
		};
		
		function folderUpdate() {
			alert("folderUpdate()");
		};
    	
		</script>
		<div id="datagridPanel" data-options="region:'center',border:false">
			<div style="padding:0px;border:none">
				<a id="newBtn" href="javascript:void(0)" class="easyui-splitbutton"></a>
				<a id="deleteBtn" href="javascript:void(0)" class="easyui-linkbutton"></a>
			</div>
			<script type="text/javascript">
			$("#newBtn").splitbutton({ text: "Nový", iconCls: "icon-new" })
			.bind("click", function(){ doAppend(); });
			$("#deleteBtn").linkbutton({ plain: true, iconCls: "icon-delete" })
			.bind("click", function(){ doDelete(); });
			</script>
			<div id="grid"></div>
			<script>
			var page = 1;
			var pageSize = 25;
			
			var h = $(window).height()-35;
			
			$("#grid").kendoGrid({
				columns: <%=columns%>,
				dataSource: <%=data%>,
				navigatable: true,
				pageable: { pageSize: 25, numeric: false },
				dataBound: function(e) {
					var grid = e.sender;
					page = grid.dataSource.page();
				},
				/*
				toolbar: [
					{ template: kendo.template($("#newBtn").html()) },
					{ template: kendo.template($("#deleteBtn").html()) }
				],
				*/
				selectable: "row",
				height: h
			});
			
			$("#grid tbody > tr").dblclick(function() {
				var grid = $("#grid").data("kendoGrid");
				var row = $(this).closest("tr");
				var item = grid.dataItem(row);
				var id = item.id;
				alert("id:" + id);
			});
			
			function doDelete() {
				var grid = $("#grid").data("kendoGrid");
				var rowIdx = grid.select().index();
				if (rowIdx > -1) {
					var item = grid.dataSource.at(rowIdx);
					var id = item.id;
					alert("id:" + id);
				}
				return false;
		    };
		    
			function doAppend() {
				alert("doAppend()");
				return false;
		    };
		    
			</script>
		</div>
	</div>
	<script type="text/javascript">
	$("#mainPanel").layout({fit: true});
	</script>
	
</body>
</html>
